<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>像素处理</title>
</head>
<body>
  <canvas id="paper" width="0" height="0"></canvas>
  <script type="module">
    import {loadImage, getImageData, traverse, getPixel} from './lib/util.js';
    import {transformColor, brightness, saturate} from './lib/color-matrix.js';

    const canvas = document.getElementById('paper');
    const context = canvas.getContext('2d');

    (async function () {
      const img = await loadImage('assets/girl1.jpg');
      const sunlight = await loadImage('assets/sunlight.png');

      const imageData = getImageData(img);
      const texture = getImageData(sunlight);

      traverse(imageData, ({r, g, b, a, index}) => {
        const texColor = getPixel(texture, index);
        return transformColor([r, g, b, a], brightness(1 + 0.7 * texColor[3]), saturate(2 - texColor[3]));
      });

      canvas.width = imageData.width;
      canvas.height = imageData.height;
      context.putImageData(imageData, 0, 0);
    }());
  </script>
</body>
</html>